<?php

namespace app\api\controller\yexam;

use app\admin\model\yexam\Fav;
use app\admin\model\yexam\QuestionLog;
use app\common\controller\Api;

/**
 * @ApiSector (章节练习接口)
 * @ApiWeight (800)
 */
class Unit extends Api
{
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = [];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];

    /**
     * @ApiTitle (获取答题卡)
     * @ApiParams (name=unit_id,type=integer,required=true,description=章节id)
     * @ApiHeaders  (name=token,type=tring,required=true,description=token)
     * @ApiMethod (POST)
     * @ApiSummary (通过章节id【unit_id】获取改章节下的试题，并对用户做题记录进行合并返回，state:1是错2是对3是未答过此题)
     */
    public function card()
    {
        $unit_id = $this->request->post('unit_id');
        $subject = new \addons\yexam\service\Question();
        $data = $subject->getTestCard($unit_id,$this->auth->id);
        $this->success('请求成功', $data);
    }
    /**
     * @ApiTitle (定位到上次)
     * @ApiSummary (用于记录上次做题的记录)
     * @ApiParams (name=unit_id,type=integer,required=true,description=章节id)
     * @ApiMethod (POST)
     */
    public function position(){
        $unit_id = $this->request->post('unit_id');
        $unit = new \addons\yexam\service\Unit();
        $this->success('请求成功',$unit->getLastPosition($unit_id,$this->auth->id));
    }

    /**
     * @ApiTitle (清空章节练习记录)
     * @ApiParams (name=unit_id,type=integer,required=true,description=章节id)
     * @ApiHeaders (name=token,type=string,required=true,description=token)
     * @ApiMethod (POST)
     */
    public function emptytestlog(){
        $request = $this->request;
        $unit_id = $request->post("unit_id",0);
        $logModel = new QuestionLog();
        $logModel->where(['user_id'=>$this->auth->id,'unit_id'=>$unit_id])->delete();
        $this->success('请求成功！');
    }
    /**
     * @ApiTitle (答题操作)
     * @ApiMethod (POST)
     * @ApiParams (name=id,type=string,required=true,description=试题id)
     * @ApiParams (name=answer,type=string,required=true,description=试题答案)
     * @ApiParams (name=type,type=integer,required=true,description=类型)
     */
    public function ansqueedit(){
        $id = $this->request->post("id",0);
        $answer = $this->request->post("answer","");
        $type = $this->request->post('type');
        $unit = new \addons\yexam\service\Unit();
        $data = $unit->doanswer($id,$answer,$this->auth->id,$type);
        $this->success('请求成功',$data);
    }
    /**
     * @ApiTitle (收藏试题)
     * @ApiMethod (POST)
     * @ApiParams (name=id,type=integer,required=true,description=试题id)
     * @ApiHeaders (name=token,type=token,required=true,description=token)
     * @ApiMethod (POST)
     */
    public function fav(){
        $id = $this->request->post("id",0);
        $question = new \addons\yexam\service\Question();
        $result = $question->favQuestion($id,$this->auth->id);
        switch ($result){
            case "1":
                $this->success('取消收藏成功');
                break;
            case "2":
                $this->success('收藏成功');
                break;
        }
    }
    /**
     * @ApiTitle (错题记录,我的错题)
     * @ApiMethod (POST)
     * @ApiParams (name=subject_id,type=integer,required=true,description=科目id)
     * @ApiParams (name=page,type=integer,required=true,description=页码)
     * @ApiParams (name=limit,type=integer,required=true,description=加载数量量)
     * @ApiHeaders (name=token,type=string,required=true,description=token)
     */
    public function error_log(){
        $subject_id = $this->request->post("subject_id",0);
        $page = $this->request->post("page");
        $limit = $this->request->post("limit");
        $question = $this->request->post('question','');
        $unitId = $this->request->post('unit_id',0);
        if(empty($subject_id)){
            $this->error('科目ID异常');
        }
        if (!empty($question))
        {
            $questionLog = new QuestionLog();
            $where['subject_id'] = $subject_id;
            $where['state'] = 0;
            $where['user_id'] = $this->auth->id;
            if ($unitId != 0) $where['unit_id'] = $unitId;
            $data = $questionLog->where($where)->field('question_id')->distinct('question_id')->page($page,$limit)->select();
            foreach ($data as $key => $value)
            {
                $data[$key] = \app\admin\model\yexam\Question::get($value['question_id']);
            }
            $total = $questionLog->where($where)->distinct('question_id')->count();
            $this->success('请求成功',[
                'data' => $data,
                'total' => $total
            ]);
        }
        $unit = new \addons\yexam\service\Unit();
        $data = $unit->getErrorLogList($subject_id,$this->auth->id,$page,$limit);
        foreach($data['data'] as &$v){
            $unit = new \addons\yexam\service\Unit();
            $arr = [];
            $v['parent_unit'] = implode("-",array_reverse($unit->getParentNames($v['id'],$arr)));
        }
        $this->success('请求成功',$data);
    }
    /**
     * @ApiTitle (收藏列表，我的收藏列表)
     * @ApiMethod (POST)
     * @ApiParams (name=subject_id,type=integer,required=true,description=科目id)
     * @ApiParams (name=page,type=integer,required=true,description=分页)
     * @ApiParams (name=limit,type=integer,required=true,description=数量限制)
     * @ApiHeaders (name=token,type=string,required=true,description=token)
     */
    public function fav_list(){
        $subject_id = $this->request->post("subject_id",0);
        $page = $this->request->post("page");
        $limit = $this->request->post("limit");
        $question = $this->request->post('question','');
        $unitId = $this->request->post('unit_id',0);
        if(empty($subject_id)){
            $this->error('科目ID异常');
        }
        if (!empty($question)){
            $where['user_id'] = $this->auth->id;
            $where['subject_id'] = $subject_id;
            if ($unitId != 0) $where['unit_id'] = $unitId;
            $fav = new Fav();
            $data = $fav->where($where)->page($page,$limit)->field('question_id')->select();
            foreach ($data as $key => $v)
            {
                $data[$key] = \app\admin\model\yexam\Question::get($v['question_id']);
            }
            $total = $fav->where($where)->count();
            $this->success('请求成功',[
                'data' => $data,
                'total' => $total
            ]);
        }
        $unit = new \addons\yexam\service\Unit();
        $data = $unit->getFavList($subject_id,$this->auth->id,$page,$limit);
        foreach($data['data'] as &$v){
            $unit = new \addons\yexam\service\Unit();
            $arr = [];
            $v['parent_unit'] = implode("-",array_reverse($unit->getParentNames($v['id'],$arr)));
        }
        $this->success('请求成功',$data);
    }

    /**
     * @ApiTitle (收藏答题卡)
     * @ApiMethod (POST)
     * @ApiParams (name=unit_id,type=integer,required=true,description=章节id)
     * @ApiHeaders (name=token,type=string,required=true,description=token)
     * @ApiReturnParams (name=msg,type=string,required=true,sample=请求成功)
     * @ApiReturn ({msg: "请求成功",data: "[]"})
     */
    public function fav_card(){
        $unit_id = $this->request->post("unit_id",0);
        $unit = new \addons\yexam\service\Unit();
        $data = $unit->getFavCard($unit_id,$this->auth->id);
        $this->success('请求成功',$data);
    }

    /**
     * 错题答题卡
     */
    public function error_card(){
        $unit_id = $this->request->post("unit_id",0);
        $unit = new \addons\yexam\service\Unit();
        $data = $unit->getErrorCard($unit_id,$this->auth->id);
        $this->success('请求成功',$data);
    }

    /**
     * @ApiTitle (移除错题)
     * @ApiParams (name=id,type=integer,required=true,description=试题id)
     * @ApiMethod (POST)
     */
    public function remove_error(){
        $id = $this->request->post('id');
        $unit = new \addons\yexam\service\Unit();
        if($unit->removeErrorQuestion($id,$this->auth->id)){
            $this->success('请求成功');
        }else{
            $this->error('请求失败');
        }
    }
    /**
     * 获取我的错题题目详情,传流水号
     */
    public function error_question(){
        $unit_id = $this->request->post('unit_id');
        $num = $this->request->post('num');
        if($num<=0){
            $this->success('请求成功',[]);
        }
        $unit = new \addons\yexam\service\Unit();
        $data = $unit->getErrorNumQuestion($unit_id,$num,$this->auth->id);
        $this->success('请求成功',$data);
    }
}
